package com.qf.forumproject.blog.service.Impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.qf.forumproject.entity.QfBlog;
import com.qf.forumproject.blog.dao.QfBlogDao;
import com.qf.forumproject.blog.service.QfBlogService;
import com.qf.forumproject.utils.BlogAndCommentVo;
import com.qf.forumproject.utils.SearchVo;
import com.qf.forumproject.utils.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * (QfBlog表)服务实现类
 *
 * @author protagonist
 * @since 2022-07-23 11:37:16
 */
@Service("qfBlogServiceImpl")
public class QfBlogServiceImpl extends ServiceImpl<QfBlogDao,QfBlog> implements QfBlogService {
    @Resource
    private QfBlogDao qfBlogDao;

    /**
     * 搜索框按条件分页模糊查询
     * @param blogSearchVo
     * @return
     */
    @Override
    public List<QfBlog> selectByPage(SearchVo blogSearchVo) {
        QueryWrapper<QfBlog> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title",blogSearchVo.getKeyWord());

        //PageHelper.startPage(blogSearchVo.getPage(),blogSearchVo.getLimit());

        return qfBlogDao.selectList(queryWrapper);
    }

    /**
     * 查询所有文章
     * @param
     * @return
     */
    @Override
    public List<ResultVo> selectAllByPage() {

        //PageHelper.startPage(blogSearchVo.getPage(),blogSearchVo.getLimit());

        return qfBlogDao.selectAll();
    }

    /**
     * 新增文章
     * @param qfBlog
     * @return
     */
    @Override
    public QfBlog addQfBlog(QfBlog qfBlog) {

        qfBlog.setBid(UUID.randomUUID().toString().replace("-",""));
        qfBlog.setGmtCreate(DateUtil.date());
        qfBlog.setGmtUpdate(DateUtil.date());

        if (this.save(qfBlog)){
            return qfBlog;
        }
        return null;
    }

    /**
     * 更新文章浏览量
     * @return
     */
    @Override
    public QfBlog updateViews(Integer id) {
        int i = qfBlogDao.updateViews(id);
        if (i > 0){
            return qfBlogDao.selectById(id);
        }
        return null;
    }


    /**
     * 根据文章id查询该文章及其所有评论
     * @param id
     * @return
     */
    @Override
    public List<BlogAndCommentVo> selectBlogAndCommentVos(Integer id) {
        return qfBlogDao.selectBlogAndCommentVos(id);
    }
}